package com.huanwei.cc.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.base.BaseActivity;
import com.huanwei.cc.bean.Paim_Bean;
import com.huanwei.mainfragment.adapter.Speach_Adapter;
import com.huanwei.map.AMapUtil;


import java.util.Locale;

/**
 * Created by zhuwenhai on 2017/12/1.
 * 首页-周边
 */

public class ZhouBian_Activity extends BaseActivity {
    @Override
    public int setLayoutId() {
        return R.layout.fragment_main2;
    }

    @Override
    public void initViewsAndEvents() {
        textToSpeech = new TextToSpeech(ZhouBian_Activity.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(ZhouBian_Activity.this, "TTS暂时不支持这种语音的朗读！",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        ListView lvList = (ListView) findViewById(R.id.lv_list);//设置;
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(ZhouBian_Activity.this, Edit2NoteActivity.class));

            }
        });
        bank_card_adapter = new Speach_Adapter();
        lvList.setAdapter(bank_card_adapter);
        bank_card_adapter.addItem(new Paim_Bean("口渴了", "以100%的健康理念，打造高品质的时尚休闲饮品，力求给消费者带来新鲜、美味、愉悦的休闲享受。产品主要有台式奶茶、鲜奶茶、茶饮、鲜榨果汁、冰沙、奶昔、咖啡等，其中“百搭奶茶”以其“美味健康、独特配方”曾经成为行业经典。", "118.4845", "32.441"));
        bank_card_adapter.addItem(new Paim_Bean("千百味骨头饭", "是彝族传统食品，骨头饭中的骨头味美肉嫩，不柴不腻，汤汁咸淡适中，口感甚佳，其中骨头饭的骨头汤是用十余种香料精心熬制成的，富含多种营养物质，也起到了很重要的美食补钙的作用，大大增强肾细胞活性，具有添骨髓，增血液，减缓衰老，强身美容，同时具有健脾补胃，增强记忆的保健的功效。", "118.4845", "32.441"));
        bank_card_adapter.addItem(new Paim_Bean("咖喱小憩", "浓郁的咖喱味道完美的融合在食材中，吃一口，那浓郁又带着椰奶香味的咖喱在嘴里爆炸，瞬间整个人犹如浸润在咖喱的世界中。", "118.4845", "32.441"));
        bank_card_adapter.addItem(new Paim_Bean("森姑传奇火锅米线", "由传统重庆火锅汤料加入传统小吃米线并配以经典重庆火锅配菜，如：鲜毛肚、豆皮、午餐肉、木耳、豆芽、方竹笋等煮制而成。", "118.4845", "32.441"));
        bank_card_adapter.addItem(new Paim_Bean("和善园", "和善园不仅为食客们带来了众多花式包子和各种用五谷杂粮精心制作的主食，还有各种粽子糕团与健康饮品，用香气与美味唤醒食客的精神和身体。", "118.4845", "32.441"));
        bank_card_adapter.addItem(new Paim_Bean("港记深井烧鹅饭", "将鹅仔处理干净，用姜末、蒜蓉、葱末、盐、白糖、料酒、玫瑰露酒、鸡精、五香粉与肉骨汤搅拌均匀，制成调味汁，将鹅腌制30分钟。用蜂蜜、白醋调成的脆皮水均匀地涂满鹅表皮，烤箱预热至230℃，烤制35分钟，然后改用180℃烤至表皮酥脆（约10~15分钟）。将烤好的鹅用刀劈开，然后剁成整齐的小块，铺在米饭上。，还有各种粽子糕团与健康饮品，用香气与美味唤醒食客的精神和身体。", "118.4845", "32.441"));


        bank_card_adapter.notifyDataSetChanged();
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Paim_Bean item = (Paim_Bean) bank_card_adapter.getItem(i);
                showDialog(item.getWeidu(), item.getJingdu(), item.getName());
            }
        });
    }


    //
    Speach_Adapter bank_card_adapter;
    TextToSpeech textToSpeech;


    private void showDialog(final String Latitude, final String Longitude, final String neirong) {
  /*
  这里使用了 android.support.v7.app.AlertDialog.Builder
  可以直接在头部写 import android.support.v7.app.AlertDialog
  那么下面就可以写成 AlertDialog.Builder
  */
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(ZhouBian_Activity.this);
        builder.setTitle("提示");
        builder.setMessage("请选择到目的地或者语音播报？");
        builder.setNegativeButton("语音播报", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (textToSpeech.isSpeaking()) {
                    textToSpeech.stop();
                }

                textToSpeech.speak(neirong,
                        TextToSpeech.QUEUE_ADD, null);
            }
        });
        builder.setPositiveButton("去目的地", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (AMapUtil.isInstallByRead("com.autonavi.minimap")) {

                    AMapUtil.goToNaviActivity(ZhouBian_Activity.this, "test", null, String.valueOf(Latitude), String.valueOf(Longitude), "1", "2");
                } else {
                    Toast.makeText(ZhouBian_Activity.this, "请安装高德地图", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.show();
    }

    @Override
    public void onDestroy() {
        if (textToSpeech != null)
            textToSpeech.shutdown();
        super.onDestroy();
    }

}
